package com.s666.demo.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.s666.demo.config.ParamsCfg;
import com.s666.demo.dao.entity.TagPlus;
import com.s666.demo.dao.entity.Ziku;
import com.s666.demo.model.dto.PeopleDTO;
import com.s666.demo.service.TagPlusService;
import com.s666.demo.service.ZikuService;
import com.s666.demo.utils.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author lijin
 */
@Controller
@RequestMapping("/study")
public class StudyController extends BaseController  {

    private static final String MENU = "menu_study";

    @Autowired
    private ZikuService zikuService;

    @Autowired
    private ParamsCfg paramsCfg;

    /**
     * 模拟异常
     * @param model
     * @return
     */
    @RequestMapping(value="/index", method = {RequestMethod.POST,RequestMethod.GET})
    public String index(HttpServletRequest request,
                        Model model, Ziku ziku,
                        @RequestParam(value="page", required=false) Integer page) {

        int startRow = page == null ? 1 : page.intValue();
        Page<Ziku> pager = zikuService.listZiku(ziku, startRow, 10 );
        PageInfo<Ziku> pageInfo = new PageInfo<Ziku>(pager);

        Map<Integer,String> zikuTypes  = paramsCfg.getZikuTypes();
        List<String> letters  = paramsCfg.getLetters();

        model.addAttribute("currentLink",getCurrentLink(request));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("ziku",ziku);
        model.addAttribute("zikuLst",pageInfo.getList());
        model.addAttribute("zikuTypes",zikuTypes);
        model.addAttribute("letters",letters);

        model.addAttribute("menu",MENU);

        return "study/index";
    }

    @Resource
    TagPlusService tagPlusService;

    @RequestMapping("/html")
    public String html(HttpServletRequest request,
                       Model model, Ziku ziku,
                       @RequestParam(value="page", required=false) Integer page) {

        int pageNum = page == null ? 1 : page.intValue();

        Page<TagPlus> pager = tagPlusService.selectUserPage( pageNum );
        PageInfo<TagPlus> pageInfo = new PageInfo<TagPlus>(pager);

        model.addAttribute("currentLink",getCurrentLink(request));
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("ziku",ziku);
        model.addAttribute("zikuLst",pager.getRecords());
        model.addAttribute("menu",MENU);

        return "study/html";
    }
    @RequestMapping("/json")
    @ResponseBody
    public PageInfo<TagPlus> json(HttpServletRequest request,
                                  Model model, Ziku ziku,
                                  @RequestParam(value="page", required=false) Integer page) {

        int pageNum = page == null ? 1 : page.intValue();
        Page<TagPlus> pager = tagPlusService.selectUserPage( pageNum );
        PageInfo<TagPlus> pageInfo = new PageInfo<TagPlus>(pager);
        return pageInfo;
    }

    @RequestMapping("/save")
    @ResponseBody
    public TagPlus save(){
        TagPlus tagPlus = tagPlusService.insert("tom", 2);

        // 比较奇怪, 原实体不会更新, 但数据库会+1;
        tagPlus.setName("18");
        boolean result = tagPlus.updateById();
        return tagPlus;
    }

    @RequestMapping("/test")
    public String test(Model model) {
        List<String> userList = new ArrayList<String>();
        userList.add("test1");
        userList.add("test2");
        model.addAttribute("userList",userList);
        model.addAttribute("menu",MENU);

        return "study/test";
    }

    @RequestMapping("/arrays")
    public String arrays(Model model){
        int [] arr  = {1,12,13,4,5};
        Arrays.sort(arr);
        String arr2str = Arrays.toString(arr);

        int idx = Arrays.binarySearch(arr, 12);
        model.addAttribute("arr2str",arr2str);
        model.addAttribute("idx",idx);
        model.addAttribute("menu",MENU);

        return "study/arrays";
    }

    /**
     * 标准页面
     * @param model
     * @return
     */
    @RequestMapping("/complex")
    public String complex(Model model) {
        model.addAttribute("menu",MENU);
        return "study/complex";
    }

    /**
     * 验证器
     * @param model
     * @return
     */
    @RequestMapping("/validator")
    public String validator(  Model model  ) {
        model.addAttribute("people", new PeopleDTO());
        return "study/validator";
    }

    @RequestMapping(value = "/post_validator",method = RequestMethod.POST)
    public String postValidator(  Model model, @Valid PeopleDTO peopleDTO  ) {
        model.addAttribute("people",peopleDTO);
        return "study/validator";
    }
}

